package com.blog.controller;

import cn.hutool.json.JSONObject;
import com.blog.common.CommonRespDto;

import com.blog.properties.CommonConfigPath;
import com.blog.seckill.business.FadadaSign;
import com.blog.seckill.business.SecBusiness;
import com.blog.seckill.business.idCardOcr;
import com.blog.service.FadSystemService;
import com.blog.service.FileSystemService;
import com.blog.service.OcrIdCardService;
import com.blog.service.SecBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SecBusinessController
 * @Destription 商户管理
 * @Author 天堂小野花
 * @Date 2025/7/4  15:37
 * @Version 1.0
 */
@RestController
@RequestMapping("/business")
public class SecBusinessController {
    @Autowired
    SecBusinessService secBusinessService;
    @Autowired
    FileSystemService fileSystemService;
    @Autowired
    OcrIdCardService ocrIdCardService;
    @Autowired
    FadSystemService fadSystemService;

    @Autowired
    CommonConfigPath commonConfigPath;

    @PostMapping("/addBusiness")
    public CommonRespDto addBusiness(@RequestBody SecBusiness secBusiness){
        if(secBusinessService.addBusiness(secBusiness)==1){
            return CommonRespDto.success("添加成功");
        }
        return CommonRespDto.error("添加失败");
    }
    @PutMapping("/updateBusiness")
    public CommonRespDto updateBusinessStatus(@RequestBody SecBusiness secBusiness){
        if(secBusinessService.updateBusinessStatus(secBusiness)==1){
            return CommonRespDto.success("更新成功");
        }
        return CommonRespDto.error("更新失败");
    }
    @GetMapping("/init")
    public CommonRespDto initBusiness(){
        List<SecBusiness> list = secBusinessService.initBusiness();
        return CommonRespDto.success(list);
    }
    @GetMapping("/fadByParentId/{rid}")
    public CommonRespDto fadByParentId(@PathVariable Integer rid){
        FadadaSign list = fadSystemService.fadByParentId(rid);
        return CommonRespDto.success(list);
    }
    @GetMapping("/idCardByParentId/{rid}")
    public CommonRespDto idCardByParentId(@PathVariable Integer rid){
        idCardOcr list = ocrIdCardService.idCardByParentId(rid);
        return CommonRespDto.success(list);
    }
    @PostMapping("/data/upload")
    public CommonRespDto ftpSendIdCard(MultipartFile file){
        if (file.isEmpty()) {
            CommonRespDto.error("请选择文件");
        }
        String path = null;
        try {
            path = fileSystemService.uploadFile("/id_card/"+file.getOriginalFilename(),file);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(!StringUtils.isEmpty(path)){
            return CommonRespDto.success(path,"上传成功");
        }else{
            return CommonRespDto.error("上传失败");
        }
    }
    @GetMapping("/data/ocr/")
    public CommonRespDto ocrIdCard( String path){
        //System.out.println(">>>>>>>>>>>>>>"+path);
        String imgUrl = "http://"+commonConfigPath.getFileHost()+":"+commonConfigPath.getFilePort()+path;
        try {
            JSONObject dataResult = ocrIdCardService.getJsonByOcr(imgUrl);
            if(dataResult != null && !dataResult.isEmpty()){
                idCardOcr  idocr = ocrIdCardService.addIdCard(dataResult);
                return CommonRespDto.success(idocr,"上传成功");

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return CommonRespDto.success(path,"上传失败");
    }
    @PostMapping("/data/fad")
    public CommonRespDto fadOrder(@RequestBody SecBusiness secBusiness) throws Exception {
        System.out.println(secBusiness.getEmail());
        Map<String, Object> result = fadSystemService.createOrder(secBusiness);

        return CommonRespDto.success(result,"等待双方签署");
    }
    /**
     * 签署完成回调
     */
    
    /**
     * @Author 天堂小野花
     * @Description 回调地址必须是可以由外网访问的 ,参考网址：https://blog.csdn.net/qq_53853500/article/details/146936948
     *                          所以需要等到将系统部署到服务器之后，才能进行测试开发
     *                          主要需要做的事情就是将 装态待签署改成已经签署
     * @Date 1:00 2025/7/20
     * @Param [headers, bizContent]
     * @return void
     **/
    //@PassToken
    @ResponseBody
    @PostMapping("/data/fadCallback")
    public void fadOrderCallback(@RequestHeader HttpHeaders headers,
                                   @RequestParam("bizContent") String bizContent) throws Exception {
        String result = fadSystemService.orderCallback(headers,bizContent);
    }
    @PostMapping("/fadCallback")
    public CommonRespDto fadCallback(@RequestBody SecBusiness secBusiness){
        if(fadSystemService.fadCallback(secBusiness)==1){
            return CommonRespDto.success("签署成功");
        }
        return CommonRespDto.error("签署失败");
    }
}
